String.prototype.replaceAll = function (FindText, RepText) {
    regExp = new RegExp(FindText, "g");
    return this.replace(regExp, RepText);
}
var he = HE.getEditor('editor', {
    html:'1',
    width: '100%',
    height: '400px',
    autoHeight: true,
    autoFloat: false,
    topOffset: 0,
    uploadPhoto: true,
    uploadPhotoHandler: '/api/file',
    uploadPhotoSize: 2048,
    uploadPhotoType: 'gif,png,jpg,jpeg',
    uploadPhotoSizeError: '不能上传大于2MB的图片',
    uploadPhotoTypeError: '只能上传gif,png,jpg,jpeg格式的图片',
    lang: 'zh-jian',
    skin: 'HandyEditor',
    externalSkin: '',
    item: ['bold', 'italic', 'strike', 'underline', 'fontSize', 'fontName', 'paragraph', 'color', 'backColor',
        'center', 'left', 'right', 'full', 'indent', 'outdent',
        'link', 'code','textBlock',  'image', 'expression', 'subscript', 'superscript',  'horizontal', 'orderedList', 'unorderedList']
});

$(function(){
	
	$('.btn_wl').click(function(){
		$(".wl_list").css({
			"display":"block",
		});
	});

	$(".wl_btn").click(function () {
        $(".wl_list").css({
            "display":"none",
        })
   	});
   	
   	$(".audio_btn").click(function(){
   		$(".audio").css({
            "display":"none",
        })
   	})
   	
   	$(".btn_music").click(function(){
   		$(".audio").css({
            "display":"block",
        })
   	})




	$("#add_img").click(function () {
		var form = new  FormData();
        var file = document.getElementById("file_img").files;
        form.append('file',file[0]);
        $.ajax({
            url:"/api/upload/file",
            type:"post",
            dataType:"json",
            data:form,
            processData:false,
            contentType:false,
            success:function(data){
                if (data.code==200){
                    $(".wl_list").css({
                        "display":"none",
                    });
                } else {
                    layer.msg("上传失败！",{icon: 5});
                }
              $(".data_img").attr("src",data.data)
            },
			error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });
    })


	$("#submit").click(function () {
        var content = he.getHtml().replaceAll(/<img/, "<img style=\"width: 100%;\"");
        $.ajax({
            type: 'POST',
            url: '/api/addarticle',
            dataType:"json",
            data: {
            	'title': $("#title").val(),
            	'articleType': $("#articleType").val(),
				'isOriginal':$("#isOriginal").val(),
				'synopsis':$("#introduction").val(),
				'content': content,
                'cover': $(".data_img")[0].src
			},
            success: function (result) {
                if (result.code==200){
                    layer.msg(result.message,{icon: 1});
                    window.setTimeout(function () {
                        window.location.href="/admin/article"
                    },1000)
                } else {
                    layer.msg(result.data.toString(),{icon: 5});
                }

            },
            error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });
    })

	/*$(document).ready(function () {
        $.ajax({
            type: 'get',
            url: '/api/music',
            success: function (result) {
                var data = result.data;
                var val = "";
                data.forEach(function(value,index){
                    val = val+"<tr><td>"+value.name+"</td>" +
                        "<td>"+value.author+"</td><td>" +
                        "<a role='button' class='btn_auditions'  onclick='playMusic(\""+value.url+"\")' style='border: 1px solid #09BB07;padding: 2px 4px;margin: 0px 4px;' >" +
                        "<span class='glyphicon glyphicon-music'></span>试听</a>" +
                        "<a role='button' class='btn_edit' onclick='choiceMusic("+value.id+")' style='border: 1px solid #F37B1D;padding: 2px 4px;margin: 0px 4px;'>" +
                        "<span class='glyphicon glyphicon-ok'></span>选择</a>" +
                        "</td></tr>";
                });
                $("#music_list").append(val)
            },
            error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });
    })*/

})

$("#file_img").change(function (e) {
    var file = document.getElementById("file_img").files;
    var freader = new FileReader();
    freader.readAsDataURL(file[0]);
    freader.onload = function (e) {
        $("#img_1").attr("src",e.target.result)
    }
})
//
// function playMusic(url) {
//     $("#myaudio").attr("src",url);
//     var myAuto = document.getElementById('myaudio');
//     myAuto.play();
// }
//
// function choiceMusic(id) {
//     $.ajax({
//         type: 'get',
//         url: '/api/music/'+id,
//         success: function (result) {
//             var data = result.data;
//             $("#music_id").html(data.id);
//             $("#music_name").html(data.name);
//             $("#music_author").html(data.author);
//             $("#music_url").html(data.url);
//             $(".audio").css({
//                 "display":"none",
//             })
//         },
//         error:function () {
//             layer.msg("系统错误！",{icon: 5});
//         }
//     });
// }

